package com.stu.thread.demo.thread_alternate_print;

public class 交替打印1 {

    public static void main(String[] args) {
        Printer printer = new Printer();
        new Thread(()->{
            printer.printNumbers();
        }).start();
        new Thread(()->{
            printer.printWord();
        }).start();
    }

}

class Printer{

    private String [] words = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","X","T","U","V","W","S","Y","Z"};

    private int [] numbers = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26};

    private int index = 1;

    private static final Object lock = new Object();


    /**
     * 打印字母
     * @return
     */
    public void printWord(){
        synchronized(lock) {
            for (String word : words) {
                System.out.print(word);
                lock.notify();
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 打印数字
     */
    public void printNumbers(){
        synchronized(lock){
            for (int number : numbers) {
                System.out.print(number);
                lock.notify();
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }





}
